package cn.iocoder.an.module.mold.service.recheckcategory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryImportExcelVO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcategory.UpkeepCategoryDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.an.module.mold.controller.admin.recheckcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckcategory.RecheckCategoryDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.recheckcategory.RecheckCategoryMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.EQUIPMENT_TYPE_NOT_EXISTS;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 复检类型 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RecheckCategoryServiceImpl implements RecheckCategoryService {

    @Resource
    private RecheckCategoryMapper recheckCategoryMapper;

    @Override
    public Long createRecheckCategory(RecheckCategorySaveReqVO createReqVO) {
        // 插入
        RecheckCategoryDO recheckCategory = BeanUtils.toBean(createReqVO, RecheckCategoryDO.class);
        recheckCategoryMapper.insert(recheckCategory);

        // 返回
        return recheckCategory.getId();
    }

    @Override
    public void updateRecheckCategory(RecheckCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateRecheckCategoryExists(updateReqVO.getId());
        // 更新
        RecheckCategoryDO updateObj = BeanUtils.toBean(updateReqVO, RecheckCategoryDO.class);
        recheckCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecheckCategory(Long id) {
        // 校验存在
        validateRecheckCategoryExists(id);
        // 删除
        recheckCategoryMapper.deleteById(id);
    }

    @Override
    public void deleteRecheckCategoryListByIds(List<Long> ids) {
        // 删除
        recheckCategoryMapper.deleteByIds(ids);
    }


    private void validateRecheckCategoryExists(Long id) {
        if (recheckCategoryMapper.selectById(id) == null) {
            throw exception(RECHECK_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public RecheckCategoryDO getRecheckCategory(Long id) {
        return recheckCategoryMapper.selectById(id);
    }


    @Override
    public Long getRecheckCategoryIdByCode(String code) {
        RecheckCategoryDO categoryDO = recheckCategoryMapper.selectFirstOne(RecheckCategoryDO::getCode, code);
        if (categoryDO != null) {
            return categoryDO.getId();
        }else {
            throw exception(RECHECK_CATEGORY_NOT_EXISTS);
        }
    }


    @Override
    public List<RecheckCategoryDO> getRecheckCategoryAll() {
        return recheckCategoryMapper.selectList();
    }


    @Override
    public PageResult<RecheckCategoryDO> getRecheckCategoryPage(RecheckCategoryPageReqVO pageReqVO) {
        return recheckCategoryMapper.selectPage(pageReqVO);
    }


    @Override
    public ImportRespVO importRecheckCategoryList(List<RecheckCategoryImportExcelVO> iomportDrawingNo, boolean isUpdateSupport) {
        ImportRespVO respVO = ImportRespVO.builder().insertNameList(new ArrayList<>())
                .updateNameList(new ArrayList<>()).failureMap(new LinkedHashMap<>()).build();

        return respVO;
    }


}